<?php

/**
 * @author Marc André Audet <root@manhim.net>
 * @copyright Copyright(c) 2009-2010 Marc André Audet
 * @license http://www.gnu.org/licenses/gpl.txt The GNU General Public License version 3
 *
 * $Rev: 152 $
 *
 *   GStats++ http://code.google.com/gstatspp/
 *
 *   Copyright (C) 2009-2010 Marc André Audet
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
if (!defined('IN_MANHIM_FRAMEWORK')) exit();

class FModule_GStatsppConfig extends Framework_Modules
{

	private $MainConfig = array('Install', 'MPDO', 'Admin', 'Plugins');

    private $_Config;
    private $_Changed = false;

    public function __construct()
    {
        $this->SetIsMainModule(false);

		if (file_exists('Config.inc.php'))
		{
			$this->_Config = unserialize(base64_decode(require 'Config.inc.php'));
		}
		else
		{
			$this->_Config = null;
		}
    }

	public function __destruct()
	{
        // TODO Write to the Config.inc.php file if there was modifications
    }

    public function SetValue($config, $value)
    {
        if (!is_array($config))
		{
			return false;
		}

		if (!in_array($config[0], $this->MainConfig))
		{
			return false;
		}

		$c_config = count($config);
		$current = array();

		for ($i = 0; $i < $c_config; $i++)
		{
			if (!isset($config[$i]))
			{
				return false;
			}

			if ($i == 0)
			{
				$current = &$this->_Config[$config[$i]];
			}
			else
			{
				$current = &$current[$config[$i]];
			}
		}

		if ($current != $value)
		{
			$current = $value;
			$this->_Changed = true;
		}
    }

	public function UnsetValue($config)
	{
		$this->SetValue($config, null);
   	}

	public function GetValue($config)
	{
        if (!is_array($config))
		{
			return null;
		}

		if (!in_array($config[0], $this->MainConfig))
		{
			return null;
		}

		$c_config = count($config);
		$current = array();

		for ($i = 0; $i < $c_config; $i++)
		{
			if (!isset($config[$i]))
			{
				return null;
			}

			if ($i == 0)
			{
				if (!isset($this->_Config[$config[$i]]))
				{
					return null;
				}
				
				$current = &$this->_Config[$config[$i]];
			}
			else
			{
				if (!isset($current[$config[$i]]))
				{
					return null;
				}

				$current = &$current[$config[$i]];
			}
		}

		return $current;
	}

    private function FetchConfigForPlugin($plugin_name)
	{
        if (isset($this->_Config['Plugins'][$plugin_name]))
        {
            return $this->_Config['Plugins'][$plugin_name];
        }
        else
        {
            return false;
        }
    }

}

?>
